<?php
namespace app\api\controller;

use app\api\model\User;
use app\api\model\Address;
use app\api\validate\AddressAddValidate;
use app\api\validate\AddressRemoveValidate;
use app\api\validate\AddressChangeValidate;
use think\exception\ValidateException;
use app\api\middleware\Auth;
use app\BaseController;

class AddressController extends BaseController
{

	protected $middleware = [Auth::class];

	// 添加地址库
	public function add(Address $model)
	{
		try {
            $data = $this->request->param();
            validate(AddressAddValidate::class)->check($data);
  
            $address = $model->createOrUpdateAddress(
            	null,
            	$this->request->userInfo->id,
            	$data['consignee'],
            	$data['mobile'],
            	$data['province'],
            	$data['city'],
            	$data['area'],
            	$data['address'],
            	$data['isDefault'],
            );
            return $this->returnJson('ok',$address);
        } catch (ValidateException $e) {
            return $this->returnJson($e->getError());
        }
	}

	// 修改地址库
	public function change(Address $model)
	{
		try {
            $data = $this->request->param();
            validate(AddressChangeValidate::class)->check($data);
  			$address = $model->find($data['addressId']);
            $model->createOrUpdateAddress(
            	$address,
            	$this->request->userInfo->id,
            	$data['consignee'],
            	$data['mobile'],
            	$data['province'],
            	$data['city'],
            	$data['area'],
            	$data['address'],
            	$data['isDefault'],
            );
            return $this->returnJson('ok');
        } catch (ValidateException $e) {
            return $this->returnJson($e->getError());
        }
	}
	
	// 删除地址库
	public function remove(Address $model)
	{
		try {
            $data = $this->request->param();
            validate(AddressRemoveValidate::class)->check($data);
  			
            $address = $model->find($data['addressId']);
            if($address->user_id != $this->request->userInfo->id){
				return $this->returnJson('地址库ID错误|301');
			}
			$address->delete();
            return $this->returnJson('ok',[]);
        } catch (ValidateException $e) {
            return $this->returnJson($e->getError());
        }
	}

	// 地址详情
	public function detail(Address $model)
	{
		$addressId = $this->request->param('addressId/d',0);

		$address = $model->find($addressId);

		if(empty($address) || $address->user_id != $this->request->userInfo->id){
			return $this->returnJson('地址库ID错误|301');
		}
		$address->provinceName= $model->getNameById($address->province);
		$address->cityName= $model->getNameById($address->city);
		$address->areaName= $model->getNameById($address->area);
		return $this->returnJson('ok',$address,1);
	}
	
	// 查看地址库
	public function lists(Address $model)
	{
		$address = $model->where('user_id',$this->request->userInfo->id);
		$data = $address->select();
		foreach($data as $value){
			$value->provinceName= $model->getNameById($value->province);
			$value->cityName= $model->getNameById($value->city);
			$value->areaName= $model->getNameById($value->area);
		}
		return $this->returnJson('ok',$data,$address->count());
	}
}